package com.web.controller;

import com.common.utils.Page;
import com.entity.*;
import com.service.LosearticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 控制器类
 */
@Controller
public class LosearticleController {
	// 依赖注入
	@Autowired
	private LosearticleService losearticleService;
	/**
	 * 分页查询
	 * 
	 */
	@RequestMapping(value = "/losearticle/list.action")
	public String list(@RequestParam(defaultValue="1")Integer page,
			@RequestParam(defaultValue="10")Integer rows,String goods, Model model,
			HttpServletRequest request,HttpSession session) {
		// 条件查询所有
		goods=request.getParameter("goods");
		if(goods==null) goods="";
		String goods1=request.getParameter("goods1");
		if(goods1==null) goods1="0";
		if(goods1.equals("1")) {
			String str=(String) session.getAttribute("goods");
			if(str!=null&&str!="") goods=str;
		}
		Page<Losearticle> losearticles = losearticleService
				.findLosearticleList(page, rows,goods);
		model.addAttribute("page", losearticles);
		session.setAttribute("goods", goods);
		return "admin/losearticle";
	}
	/**
	 * 通过id获取信息
	 */
	@RequestMapping("/losearticle/getLosearticleById.action")
	@ResponseBody
	public Losearticle getLosearticleById(Integer id) {
	    Losearticle losearticle = losearticleService.getLosearticleById(id);
	    return losearticle;
	}
	
	/**
	 * 创建
	 */
	@RequestMapping("/losearticle/create.action")
	@ResponseBody
	public String losearticleCreate(Losearticle losearticle,HttpSession session) {
		if(losearticle.getGoods()==""||losearticle.getGoods()==null) {
			return "goods";
		}
		if(losearticle.getContent()==""||losearticle.getContent()==null) {
			return "content";
		}
		losearticle.setUserid("0000000000");
		Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String today = sdf.format(d);
		losearticle.setTimes(today);
		String article="";
		article=losearticle.getContent().replaceAll("\r\n", "");
		losearticle.setContent(article);
	    // 执行Service层中的创建方法，返回的是受影响的行数
	    int rows = losearticleService.createLosearticle(losearticle);
	    if(rows > 0){
	        return "OK";
	    }else{
	        return "FAIL";
	    }
	}
	/**
	 * 更新
	 */
	@RequestMapping("/losearticle/updateLosearticle.action")
	@ResponseBody
	public String losearticleUpdate(Losearticle losearticle) {
		System.out.println(losearticle.getTimes());
		if(losearticle.getGoods()==""||losearticle.getGoods()==null) {
			return "goods";
		}
		if(losearticle.getContent()==""||losearticle.getContent()==null) {
			return "content";
		}
		String article="";
		article=losearticle.getContent().replaceAll("\r\n", "");
		losearticle.setContent(article);
		int rows = losearticleService.updateLosearticle(losearticle);
		if(rows > 0){
		    return "OK";
		}else{
		    return "FAIL";
		}
	}
	@RequestMapping("/losearticle/updateState.action")
	@ResponseBody
	public String Updatesatate(Losearticle losearticle) {
		int rows = 0;
		Losearticle losearticle1 = losearticleService.getLosearticleById(losearticle.getId());
		System.out.println(losearticle1.getState());
		if(losearticle1.getState().equals("未认领/未找回")) {
			losearticle.setState("1");
			rows = losearticleService.updateState(losearticle);
		}
		else {
			losearticle.setState("0");
			rows = losearticleService.updateState(losearticle);
		}
		if(rows > 0){
		    return "OK";
		}else{
		    return "FAIL";
		}
	}
	/**
	 * 删除
	 */
	@RequestMapping("/losearticle/delete.action")
	@ResponseBody
	public String losearticleDelete(Integer id) {
	    int rows = losearticleService.deleteLosearticle(id);
	    if(rows > 0){			
	        return "OK";
	    }else{
	        return "FAIL";			
	    }
	}

}
